/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import com.lmx.SessionFilter;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.tmodel.NursingPlanServiceItem;
import com.lmx.repoDomain.tmodel.NursingProjectServiceItem;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(NursingProjectServiceItem.class)
@Component
public class NursingProjectItemRepository extends BaseRepository<NursingProjectServiceItem> {
    private static Log logger = LogFactory.getLog(SessionFilter.class);
	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(NursingProjectServiceItem.class);
	}

	@Override
	@JsonApiFindAll 
	public Iterable<NursingProjectServiceItem> findAll(final QueryParams requestParams) {
		String action = ActionContextHolder.getContext().getAction();
		ArrayList<NursingProjectServiceItem> itemList = new ArrayList<NursingProjectServiceItem>();
		if ("spec_countService".equals(action)) {
			Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
			long uid = -1;//user id  移动端员工查看自己定时任务
			if(map.get("uid")!=null&&!"".equals(map.get("uid"))){
				uid= Long.parseLong(map.get("uid")); 
			}
			if (uid != -1){
				return nursingService.getStaffProjectItem(uid);
			}
			long cid = -1;// customer id  移动端员工查看老人  定时服务
			if(map.get("cid")!=null&&!"".equals(map.get("cid"))){
				cid= Long.parseLong(map.get("cid")); 
			}
			if (cid != -1){
				return nursingService.getCustomerProjectItem(cid);
			}
			return itemList;
		} 
		
		if ("query_fromDetail".equals(action)) {
			Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
			long cid = -1;// customer id
			if (map.get("cid") != null && !"".equals(map.get("cid"))) {
				cid = Long.parseLong(map.get("cid"));
			}
			if (cid != -1) {
				return nursingService.getProjectItemFromPlanDetail(cid, CommonUtil.getCurrTimes());
			}
			return itemList;
		}
		
		Long tenantId = ActionContextHolder.getContext().getTenantId();
		if (tenantId != null) {
			return dslJpaService.findAllBusi(requestParams, tenantId);
		} else {
			return dslJpaService.findAll(requestParams);
		}
		
	}
}
